package com.me.libgdx_test;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;

public class ClassTexturePackerLoader {
	TextureAtlas atlas;
	public ArrayList<ClassSpriteSheet> sprites;
	Texture spriteMap;
	private ClassAnimationTextureLoader animations;
	private ClassLogDisplay logger;
	
	public ClassTexturePackerLoader(String textFile,String imageFile,float rotation, ClassAnimationTextureLoader animations){
		logger = new ClassLogDisplay("ClassTexturePackerLoader");
		atlas = new TextureAtlas(textFile);
		sprites = new ArrayList <ClassSpriteSheet>();
		spriteMap = new Texture(Gdx.files.internal(imageFile));
		this.animations = animations;
		separate_sprites(rotation);
	}

	private void separate_sprites(float rotation){
		Sprite holderSprite;
		//Seperate the sprites from the spritesheet
		for(AtlasRegion region: atlas.getRegions()){
			holderSprite = new Sprite(spriteMap,region.getRegionX(),region.getRegionY(),region.getRegionWidth(),region.getRegionHeight());
			holderSprite.rotate(rotation);
			sprites.add(new ClassSpriteSheet(holderSprite,region.name));
		}
		//Add the sprites from the animations
		for(ClassAnimationSequence animation : animations.get_all_animation_sequences()){
			for(int i = 0; i < animation.get_sequence_sprites().size(); i++){
				sprites.add(new ClassSpriteSheet(animation.get_sequence_sprites().get(i),animation.get_sequence_strings().get(i)));
			}
			animation.sort_frames();
		}
	}
	
	public ArrayList<Sprite> return_all_by_name(String name_part){
		ArrayList<Sprite> return_sprites = new ArrayList<Sprite>();
		for(int i = 0; i < sprites.size();i++){
			//logger.print("Found: "+ sprites.get(i).name);
			if(sprites.get(i).get_name().indexOf(name_part)!=-1){
				return_sprites.add(sprites.get(i).get_sprite());
				//holder.add(sprites.get(i).get_sprite());
			}
		}
		return return_sprites;
	}
	
	public Sprite find_by_name(String name){
		//ArrayList<Sprite> holder = new ArrayList<Sprite>();
		//logger.print("Looking for: "+ name);
		for(int i = 0; i < sprites.size();i++){
			/*if(name.contains("altar")){
				logger.print("Found: "+ sprites.get(i).name);
			}*/
			if(name.indexOf(sprites.get(i).get_name())!=-1){
				/*if(name.contains("altar")){
					logger.print("Returning: "+ sprites.get(i).name);
				}*/
				return sprites.get(i).get_sprite();
				//holder.add(sprites.get(i).get_sprite());
			}
		}
		return sprites.get(0).get_sprite();
	}
	
	public void add_sprite(Sprite sprite, String name){
		sprites.add(new ClassSpriteSheet(sprite,name));
	}
	
	public ClassAnimationTextureLoader get_animations(){
		return animations;
	}
	
	public class ClassSpriteSheet{
		private Sprite sprite;
		private String name;
		ClassSpriteSheet(Sprite sprite, String name){
			this.sprite = sprite;
			this.name = name;
		}
		public String get_name(){
			return name;
		}
		public Sprite get_sprite(){
			return sprite;
		}
	}
}